package com.yima.baseproj.module.app.file;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.yima.baseproj.config.application.ApplicationUtil;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/api/app/file")
public class FileUpController {

    @ApiOperation("上传一个文件")
    @PostMapping(value = "/up")
    public R<Object> up(@RequestParam("file") MultipartFile file, String rootPath) throws IOException {
        if (!file.isEmpty()) {
            return R.success(saveFile(file, rootPath), "上传成功");
        } else {
            throw ApiException.getInstance("上传失败");
        }
    }

    @ApiOperation("上传多个文件")
    @PostMapping(value = "/ups")
    public Object ups(@RequestParam("files") MultipartFile[] files,String rootPath) throws IOException {
        if(files.length > 0){
            List<Map<String,String>> list = ListUtil.toList();
            for (MultipartFile multipartFile : files) {
                list.add(saveFile(multipartFile,rootPath));
            }
            return list;
        }
        throw ApiException.getInstance("上传失败");
    }

    /**
     * 保存文件到本地
     * @param multipartFile
     * @param rootPath
     * @return
     * @throws IOException
     */
    private Map<String,String> saveFile(MultipartFile multipartFile, String rootPath) throws IOException {
        String fileName = multipartFile.getOriginalFilename();

        //新文件名
        String newFileName = String.format("%s.%s", IdUtil.simpleUUID(), FileUtil.getSuffix(fileName));

        //相对路径和绝对路径
        String newRelativePath = String.format("%s/%s", rootPath, newFileName);
        String realPath = String.format("%s/%s", ApplicationUtil.uploadLocalPath, newRelativePath);

        //创建文件夹
        cn.hutool.core.io.FileUtil.mkParentDirs(realPath);

        //保存文件到本地
        multipartFile.transferTo(new File(realPath));

        return getResult(newFileName,newRelativePath,String.format("%s/%s", ApplicationUtil.uploadWebBasepath, newRelativePath));
    }

    /**
     * 上传文件结果
     * @param name
     * @param path
     * @param url
     * @return
     */
    private Map<String,String> getResult(String name,String path,String url){
        Map<String,String> result = MapUtil.newHashMap();
        result.put("name",name);//文件名
        result.put("path",path);//相对路径
        result.put("url",url);//绝对路径

        return result;
    }
}